package cook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cook.enums.BizCodeEnum;
import cook.mapper.FridgeGoodsMapper;
import cook.model.FridgeGoodsDO;
import cook.request.FridgeGoodsIdRequest;
import cook.request.FridgeGoodsModRequest;
import cook.request.FridgeGoodsRequest;
import cook.request.FridgeIdRequest;
import cook.service.FridgeGoodsService;
import cook.util.JsonData;
import cook.vo.FridgeGoodsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 冰箱里的食材表 服务实现类
 * </p>
 *
 * @author 爱计算机的小天才
 * @since 2023-03-29
 */
@Service
@Slf4j
public class FridgeGoodsServiceImpl implements FridgeGoodsService {

    @Autowired(required = false)
    private FridgeGoodsMapper fridgeGoodsMapper;

    /**
     * 往冰箱添加食材
     * @param fridgeGoodsRequest
     * @return
     */
    @Override
    public JsonData add(FridgeGoodsRequest fridgeGoodsRequest) {
        try {
            FridgeGoodsDO fridgeGoodsDO=new FridgeGoodsDO();
            BeanUtils.copyProperties(fridgeGoodsRequest, fridgeGoodsDO);
            fridgeGoodsDO.setCreateTime(new Date());

            fridgeGoodsMapper.insert(fridgeGoodsDO);
//            log.info("食材：{}",fridgeGoodsDO);

            return JsonData.buildSuccess(BizCodeEnum.ADD_FRIDGE_GOODS_SUCCESS.getMessage());
        }catch (Exception e) {
            return JsonData.buildResult(BizCodeEnum.ADD_FRIDGE_GOODS_ERROR);
        }



    }

    /**
     * 删除冰箱里的食材
     * @param fridgeGoodsIdRequest
     * @return
     */
    @Override
    public JsonData del(FridgeGoodsIdRequest fridgeGoodsIdRequest) {
        try {
            FridgeGoodsDO fridgeGoodsDO = fridgeGoodsMapper.selectById(fridgeGoodsIdRequest.getId());
            fridgeGoodsDO.setIsDel(1);
            fridgeGoodsMapper.updateById(fridgeGoodsDO);
            return JsonData.buildSuccess(BizCodeEnum.DEL_FRIDGE_GOODS_SUCCESS.getMessage());
        }catch (Exception e) {
            return JsonData.buildResult(BizCodeEnum.DEL_FRIDGE_GOODS_ERROR);
        }
    }

    /**
     *  修改食材信息
     */
    @Override
    public JsonData mod(FridgeGoodsModRequest fridgeGoodsModRequest) {
        try {
            FridgeGoodsDO fridgeGoodsDO=new FridgeGoodsDO();
            BeanUtils.copyProperties(fridgeGoodsModRequest, fridgeGoodsDO);
            fridgeGoodsMapper.updateById(fridgeGoodsDO);

            return JsonData.buildSuccess(BizCodeEnum.MOD_FRIDGE_GOODS_SUCCESS.getMessage());
        }catch (Exception e) {
            return JsonData.buildResult(BizCodeEnum.MOD_FRIDGE_GOODS_ERROR);
        }
    }

    /**
     * 查看食材详情
     * @param fridgeGoodsIdRequest
     * @return
     */
    @Override
    public JsonData detail(FridgeGoodsIdRequest fridgeGoodsIdRequest) {
        try {
            FridgeGoodsDO fridgeGoodsDO = fridgeGoodsMapper.selectById(fridgeGoodsIdRequest.getId());
            FridgeGoodsVO fridgeGoodsVO=new FridgeGoodsVO();

            if(fridgeGoodsDO.getIsDel() != 1) {
                BeanUtils.copyProperties(fridgeGoodsDO, fridgeGoodsVO);
                return JsonData.buildSuccess(fridgeGoodsVO);
            }else {
                return JsonData.buildSuccess();
            }
        }catch (Exception e) {
            return JsonData.buildResult(BizCodeEnum.QUERY_FRIDGE_GOODS_ERROR);
        }
    }

    /**
     * 查询食材列表
     * @param fridgeIdRequest
     * @return
     */
    @Override
    public JsonData list(FridgeIdRequest fridgeIdRequest) {
        try {
            List<FridgeGoodsDO> fridgeGoodsDOList = fridgeGoodsMapper.selectList(new QueryWrapper<FridgeGoodsDO>()
                    .eq("fridge_id", fridgeIdRequest.getId())
                    .eq("is_del", 0)
            );

            List<FridgeGoodsVO> fridgeGoodsVOList = fridgeGoodsDOList.stream().map(obj -> {
                FridgeGoodsVO fridgeGoodsVO = new FridgeGoodsVO();
                BeanUtils.copyProperties(obj, fridgeGoodsVO);
                return fridgeGoodsVO;
            }).collect(Collectors.toList());

            return JsonData.buildSuccess(fridgeGoodsVOList);
        }catch (Exception e) {
            return JsonData.buildResult(BizCodeEnum.LIST_FRIDGE_GOODS_ERROR);
        }
    }



}
